$(function(){
	$('.f_passwordRemember').click(function(){
		var bool = $(this).children('input').val();
		if(bool=='false'){
			$(this).children('.f_ico').addClass('f_icoTrue');
			$(this).children('input').val('true');
		}else if(bool=='true'){
			$(this).children('.f_ico').removeClass('f_icoTrue');
			$(this).children('input').val('false');
		}
	});
	
	//Create login Obj Class
	var login = new Object();

	login.userLoginSubmitBtn = $("#userLoginSubmitBtn");
	login.passwordRemember = $("#passwordRemember");
	login.userName="";
	login.passWord="";
	login.userLoginSubmitBtn.live("click",function(){
		Verification.checkData($("#username"));
		Verification.checkData($("#password"));
		var allowLogin = $(this).attr("allowLogin");
		if(allowLogin=="true") login.checkAjax();
	});
	$('.f_listBoxs > .f_ll > input').keydown(function(e){
		if(e.keyCode==13){
			Verification.checkData($("#username"));
			Verification.checkData($("#password"));
			var allowLogin = login.userLoginSubmitBtn.attr("allowLogin");
			if(allowLogin=="true") login.checkAjax();
		}
	});
	
	// Get ip
//	var ipUrl = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_='+Math.random();  
//	var loginIp = "";
//	$.getJSON(ipUrl, function(data){
//		loginIp = data.Ip;
//	});
	
	// Get username or password
	login.getFormData=function(){
		login.userName="";
		login.passWord="";		
		login.userName = $("#username").val();
		login.passWord = $("#password").val();
		var jsons = {
			"loginSource":'web',
			"account":login.userName,
			"password":login.passWord
//			,"loginIp":loginIp
		};
		//console.log(JSON.stringify(jsons));
		return jsons;
	};
	// Connect Ajax Database
	login.checkAjax = function(){
		$.ajax({
	        type: "POST",
	        url: basePath+"user/login.do",
	        data: login.getFormData(),
	        success: function (data){
	            if (data.returnCode==10000) {
	            	$(".f_loginPrompt").show().html("<span class='f_ico f_icoTrue'></span> <font color='#1cb99a'>"+data.returnMessage+"</font>");
	            	var returnPageUrl = $.cookie("returnPageUrl");
	            	if(returnPageUrl==null) returnPageUrl = basePath+"index.jsp?parId=0";
	            	var urls = returnPageUrl;
	            	setTimeout("javascript:location.href='"+urls+"'",1000);
	            	login.savaCookies();
	            }else{
	            	$(".f_loginPrompt").show().html("<span class='f_ico f_icoFalse'></span> <font>"+data.returnMessage+"</font>");
	            	$("#password").focus();
	            	$("#password").parent().siblings(".f_rr").addClass("f_icoFalse");
	            }
	        },
	        error: function (message) {
	          	 layer.msg("登录失败",2,-1);
	        }
	    });			
	};
	
	//Sava cookies
	login.savaCookies=function(){
		var rememberUserLoginInfo = $("#rememberUserLoginInfo").val();
		if(rememberUserLoginInfo=="true"){
				//如果你想在整个网站中访问这个cookie需要这样设置有效路径：path: '/'。
				$.cookie("rmbUser", "true", { expires: 7,path:'/'}); 
				$.cookie("username", login.userName, { expires: 7,path:'/'}); 
				$.cookie("password", login.jiajiajia(login.passWord), { expires: 7,path:'/'}); 
			
		}
		if(rememberUserLoginInfo=="false"){
			$.cookie("rmbUser",null,{ path: '/' });
			$.cookie("username",null,{ path: '/' });
			$.cookie("password",null,{ path: '/' });
		}
	};
	//Load rmbUser info 
	login.setRmbUser = function(){
		var cookrmbUser = $.cookie("rmbUser");
		if(cookrmbUser=="true"){
			var cookName = $.cookie("username");
			var cookpassword = login.jiejiejie($.cookie("password"));
			$("#username").val(cookName);
			$("#password").val(cookpassword);
			$("#userLoginForm .ttt").hide();
			$(".f_passwordRemember").children('.f_ico').addClass('f_icoTrue');
			$(".f_passwordRemember").children('input').val('true');
		}
	};

	//set jjj
	login.jiajiajia=function(val){
	    num_out = "";
	    str_in = escape(val);
	    for(i = 0; i < str_in.length; i++) {
	    num_out += str_in.charCodeAt(i) - 23;
	    }
	    return num_out;
	}
	//set jjj
	login.jiejiejie=function(val) {
	    str_out = "";
	    num_out = val;  
	    for(i = 0; i < num_out.length; i += 2) {
	    num_in = parseInt(num_out.substr(i,[2])) + 23;
	    num_in = unescape('%' + num_in.toString(16));
	    str_out += num_in;
	    }
	    var str_out_jm = unescape(str_out);
	    return str_out_jm ;
	}	
	
	//Initialization load rmbUser cookies info 
	login.setRmbUser();

	//登陆框事件监测
	var oldVal = '';
	var objInput = $('.f_listBoxs > .f_ll > input');
	objInput.click(function(){
		$(this).siblings('.ttt').hide();
		$(this).focus();		
	});
	objInput.blur(function(){
		var thisVal = $(this).val();
		if(thisVal==null||thisVal==""){
			$(this).siblings('.ttt').show();
		}
	});

	//输入状态监控
	objInput.keyup(function(){
		Verification.checkData($(this));
	});
	$("#username").keyup(function(){
		if($.cookie("rmbUser")=="true"&&$.cookie("username")==$("#username").val()){
			$("#password").val(login.jiejiejie($.cookie("password")));
			$("#password").siblings(".ttt").hide();
		}else{
			$("#password").val("");
			$("#password").siblings(".ttt").show();
		}
	});
	
	var Verification = {
		//最小字符数判断
		'minSize':function(minNum,lengthOf){
			if(minNum>lengthOf)return true;
			if(minNum<=lengthOf)return false;
		},
		//最大字符数判断
		'maxSize':function(maxNum,lengthOf){
			if(maxNum<lengthOf)return true;
			if(maxNum>=lengthOf)return false;
		},
		//控制提示打印
		'promptTiShi':function(_this,content,className){
			var conClass = " 0ff";
			if(className=='f_icoFalse')conClass='f_icoTrue';
			if(className=='f_icoTrue')conClass='f_icoFalse';
			_this.parent().siblings('.f_ico').addClass(className).removeClass(conClass).html(content);
		},
		//不正确的时候设置函数
		'promptFocus':function(_this){
			_this.siblings('.ttt').hide();
			_this.focus();
		},
		"checkData":function(_this){
			var lengthOf = _this.val().length;
			var dLength = _this.attr('maxlength');
			var minLength = _this.attr('minlength');
			if(Verification.maxSize(dLength,lengthOf)){
				Verification.promptTiShi(_this,"长度不能超过50个字符！",'f_icoFalse');
				Verification.promptFocus(_this);
				$("#userLoginSubmitBtn").attr("allowLogin","false");
			}else if(Verification.minSize(minLength,lengthOf)){
				Verification.promptTiShi(_this,"长度不能小于"+minLength+"个字符！",'f_icoFalse');
				Verification.promptFocus(_this);
				$("#userLoginSubmitBtn").attr("allowLogin","false");
				$(".f_loginPrompt").hide();
			}else{
				Verification.promptTiShi(_this," ",'f_icoTrue');
				$("#userLoginSubmitBtn").attr("allowLogin","true");
			};
			if(lengthOf<1){
				Verification.promptTiShi(_this,"不能为空",'f_icoFalse');
				Verification.promptFocus(_this);
				$("#userLoginSubmitBtn").attr("allowLogin","false");
			};
		}
	};
});




